package com.lzx.adx.report.util.helper;

import cn.hutool.core.util.URLUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.model.huawei.request.HwReportRequest;
import com.lzx.adx.report.model.wifi.WifiRequest;
import com.lzx.adx.report.model.youyou.YouYouRequest;
import springfox.documentation.spring.web.json.Json;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class YiduiHelper {


    public static String covertFinalUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        String callBackUrl=String.format(accountDTO.getDspCallbackUrl(),reportDTO.getBizRequestId());

        WifiRequest wifiRequest= JSON.parseObject(reportDTO.getExtend(),WifiRequest.class) ;

        JSONObject jsonObject=JSONObject.parseObject(accountDTO.getDspExtend());

        Map<String,Object> params=new LinkedHashMap<>();
        params.put("callback",callBackUrl);
        params.put("os","0");
        params.put("ip",reportDTO.getIp());
        params.put("ua",reportDTO.getUser_agent());
        params.put("click_id",reportDTO.getBizRequestId());
        params.put("androidid_md5",wifiRequest.getImeiMd5());
        params.put("androidid",wifiRequest.getHash_android_id());
        params.put("cid","");
        params.put("aid",jsonObject.getString("channelId"));
        params.put("oaid",wifiRequest.getOaid());
        params.put("ts_ms",System.currentTimeMillis());


        String paramsUrl = URLUtil.buildQuery(params, StandardCharsets.UTF_8);

        return accountDTO.getDspReportUrl()+"?"+paramsUrl;
    }


    public static String covertHWUrl(AccountDTO accountDTO, HwReportRequest hwReportRequest,ReportDTO reportDTO) {
        String callBackUrl=String.format(accountDTO.getDspCallbackUrl(),reportDTO.getBizRequestId());
        JSONObject jsonObject=JSONObject.parseObject(accountDTO.getDspExtend());
        Map<String,Object> params=new LinkedHashMap<>();
        params.put("callback",callBackUrl);
        params.put("os","0");
        params.put("ip",reportDTO.getIp());
        params.put("ua",reportDTO.getUser_agent());
        params.put("click_id",reportDTO.getBizRequestId());
        params.put("cid","");
        params.put("aid",jsonObject.getString("channelId"));
        params.put("oaid",hwReportRequest.getOaid());
        params.put("ts_ms",System.currentTimeMillis());
        String paramsUrl = URLUtil.buildQuery(params, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl()+"?"+paramsUrl;
    }

    public static String covertYouYouUrl(AccountDTO accountDTO, YouYouRequest youYouRequest, ReportDTO reportDTO) {
        String callBackUrl=String.format(accountDTO.getDspCallbackUrl(),reportDTO.getBizRequestId());
        JSONObject jsonObject=JSONObject.parseObject(accountDTO.getDspExtend());
        Map<String,Object> params=new LinkedHashMap<>();
        params.put("callback",callBackUrl);
        params.put("os","0");
        params.put("ip",reportDTO.getIp());
        params.put("ua",reportDTO.getUser_agent());
        params.put("click_id",reportDTO.getBizRequestId());
        params.put("cid","");
        params.put("aid",jsonObject.getString("channelId"));
        params.put("oaid",reportDTO.getOaid());
        params.put("ts_ms",System.currentTimeMillis());
        String paramsUrl = URLUtil.buildQuery(params, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl()+"?"+paramsUrl;
    }
}
